package org.dant.ant.extension.net.processors;


import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.StringUtils;

public class RemoteLogger extends DefaultLogger {
  
    protected StringBuffer outBuffer = new StringBuffer();
    protected StringBuffer errBuffer = new StringBuffer();
    protected String target;
    private boolean newTarget = true;
  
    public RemoteLogger() {
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public void targetStarted(BuildEvent event) {
        if (!target.equals(event.getTarget().getName())) {
            return;
        }
        log("<" + event.getTarget() + "> target started");
        if (Project.MSG_INFO <= msgOutputLevel) {
            String msg = StringUtils.LINE_SEP 
                + event.getTarget().getName() + ":";
            printMessage(msg, out, event.getPriority());
            log(msg);
        }
        newTarget = false;
    }
  
    public void targetFinished(BuildEvent event) {
        newTarget = true;
        if (!target.equals(event.getTarget().getName())) {
            return;
        }
        log("<" + event.getTarget() + "> target finished");
    }
  
    public void taskStarted(BuildEvent event) {
    }
  
    public void taskFinished(BuildEvent event) {
    }
  
    public void messageLogged(BuildEvent event) {
        if (event.getTarget() == null || !target.equals(event.getTarget().getName()))  {
            return ;
        }
    
        log(event.getMessage());
    }
  
    protected void log(String message) {
        if (!newTarget) {
            outBuffer.append("\t");
        }
        outBuffer.append(message);
        outBuffer.append("\n");
    }

    public StringBuffer getOutBuffer() {
        return outBuffer;
    }
}

